<?php
class m_jobwanted extends Zmodel{
	function __construct() {
		parent::__construct();
		$this->load->database();
	}
	
	function  insert_job($job_wan_sub,$job_wan_desc){
        $Z_USER=Zsession::getUserFromSession();
        $user_id=$Z_USER['user_id'];
       	$sql="INSERT INTO job_wanted (user_id,
						    job_wan_subject,
						    job_wan_desc,
						    date_create) 
       				VALUES(
       					 ".$user_id.",
       					 '".pg_escape_string($job_wan_sub)."',
       					 '".pg_escape_string($job_wan_desc)."',
       					 Now())";      	
       	$this->db->query($sql);
    }
	function insert_file($file_name,$file_path){
        $sql="INSERT INTO job_w_file (file_name,file_path) VALUES ('".$file_name."','".$file_path."')";
        $this->db->query($sql);
    }
  	
    function select_max_file_id(){
    	$sql="SELECT MAX(file_id) AS file_id FROM job_w_file";
    	$query=$this->db->query($sql);
    	$myfile=$query->result();
    	$fileid=$myfile[0]->file_id;
    	return $fileid;
    }
    
   function select_max_job_id(){
    	$sql="SELECT MAX(job_wan_id) AS job_id FROM job_Wanted";
    	$query=$this->db->query($sql);
    	$myjob=$query->result();
    	$jobid=$myjob[0]->job_id;
    	return $jobid;
    }
    
    function insertUserFile($job_id,$file_id){
        $sql="INSERT INTO job_w_user_file (job_wan_id,file_id) VALUES ('".$job_id."','".$file_id."')";
        $this->db->query($sql);
    }
    
    function get_all_job_wanted($id=null) {
    	$con=($id==null)?"":"WHERE J.job_wan_id=".$id;
    	$sql = "SELECT J.job_wan_id,
    				   J.job_wan_subject,
    				   J.job_wan_desc,
    				   J.date_create,
    				   J.user_id,
    				   U.user_login,
    				   U.user_photo
    			FROM job_wanted AS J 
    			INNER JOIN users AS U ON J.user_id=U.user_id
    			".$con." ORDER BY J.job_wan_id DESC ";
    	return $this->db->query($sql);   	
    }
    
    function get_job_wanted($num,$offset) {
    	
		$offset = isset($offset) && $offset!="" ? $offset : "0";
    	$sql = "SELECT J.job_wan_id,
    				   J.job_wan_subject,
    				   J.job_wan_desc,
    				   J.date_create,
    				   J.user_id,
    				   U.user_login,
    				   U.user_photo
    			FROM job_wanted AS J 
    			INNER JOIN users AS U ON J.user_id=U.user_id
    			ORDER BY J.job_wan_id DESC LIMIT ".$num." OFFSET ".$offset."";
    	return $this->db->query($sql);   	
    }
    
    function get_current_job_wanted() {
    	$Z_USER = Zsession::getUserFromSession();
	    $userid=$Z_USER["user_id"];
    	$sql = "SELECT J.job_wan_id,
    				   J.job_wan_subject,
    				   J.job_wan_desc,
    				   J.date_create,
    				   J.user_id,
    				   U.user_login,
    				   U.user_photo
    			FROM job_wanted AS J 
    			INNER JOIN users AS U ON J.user_id=U.user_id
    			WHERE J.user_id=".$userid."
    			ORDER BY J.date_create DESC ";
    	return $this->db->query($sql);   	
    }
    
    function get_job_wanted_main1() {
    	$sql = "SELECT job_wanted.job_wan_id,
				       job_wanted.job_wan_subject,
				       job_wanted.job_wan_desc,
				       job_wanted.user_id,
				       (SELECT user_login FROM users u,job_wanted WHERE job_wanted.user_id = u.user_id  LIMIT 1) as user_login,
				       (SELECT user_photo FROM users u,job_wanted WHERE job_wanted.user_id = u.user_id  LIMIT 1) as user_photo,
				       job_wanted.date_create
    			FROM  job_wanted 
    			ORDER BY job_wanted.job_wan_id DESC LIMIT 5";
    	return $this->db->query($sql);   	
    }
    function get_job_wanted_main() {
    	$sql = "SELECT J.job_wan_id,
    				   J.job_wan_subject,
    				   J.job_wan_desc,
    				   J.date_create,
    				   J.user_id,
    				   U.user_login,
    				   U.user_photo
    			FROM job_wanted AS J 
    			INNER JOIN users AS U ON J.user_id=U.user_id
    			ORDER BY J.job_wan_id DESC LIMIT 5";
    	return $this->db->query($sql);   	
    }
    			
    /* Select File To Show Page want_job */
	function select_file($job_wan_id){
            $sql="SELECT F.file_name, F.file_path, F.file_id FROM job_w_file AS F INNER JOIN job_w_user_file AS J ON F.file_id=J.file_id WHERE J.job_wan_id=".$job_wan_id."";
     		$query=$this->db->query($sql);
            
            return $query;
    }
    
 	//function select_upload($uid){        
    //     $sql="SELECT p.job_wan_id FROM job_wanted AS p INNER JOIN job_w_file AS p ON p.file_id=p.job_wan_id WHERE p.job_wan_id=".$uid."";
    //     $query=$this->db->query($sql);
    //     $result=$query->result();
    //     return $result;
    //}
    /* Select DESC To Show Page want_job */
	function  select_upload($job_wan_id=null){
        if($job_wan_id!=null){
            $sql="SELECT * FROM job_wanted WHERE job_wan_id=".$job_wan_id." ORDER BY job_wan_id DESC";
        }else{
            $sql="SELECT * FROM job_wanted ORDER BY job_wan_id DESC";
        }
            $query=$this->db->query($sql);
            $result=$query->result();
            return $result;
    }
    
   // function select_file_upload($fid){
   // 	$sql="SELECT * FROM job_w_file WHERE file_id=".$fid."";
   // 	$query=$this->db->query($sql);
   // 	$result=$query->result();
   // 	return $result;
   // }
    
	function update_job($jid,$jdesc,$jsub){
      $this->load->database();     
      $sql="UPDATE job_wanted SET job_wan_desc='".pg_escape_string($jdesc)."',job_wan_subject='".pg_escape_string($jsub)."' WHERE job_wan_id=".$jid;     
      $this->db->query($sql);
    }
    
	function delete_job($uid){
        $this->load->database();
        $sql="DELETE FROM job_wanted WHERE job_wan_id=".$uid."";
        $this->db->query($sql);
    }
    function delete_f_edit($file_id){      
        $sql="DELETE FROM job_w_file WHERE file_id=".$file_id."";
        $this->db->query($sql);
    }
    function delete_file($file_id){        
        $sql="DELETE FROM job_w_user_file WHERE file_id=".$file_id."";
        $this->db->query($sql);
    }
    
    /* function coun = 0 show upload file again */
    
    function count_file($job_id){
    	$sql="SELECT * FROM job_w_user_file WHERE job_wan_id=".$job_id;
    	$rst=$this->db->query($sql);
    	$filenum=0;                                 /* if 0 show Browse Upload file again */
    	foreach($rst->result_array() as $myrst){
    		$filenum++;                             /* if not 0 it Loop */
    	}
    	return $filenum;
    }
    
	function get_jobwanted_hit($id){
		$this->load->database();
		$sql="SELECT job_wanted_id
			  FROM job_wanted_hits 
			  WHERE job_wanted_id=".$id;
		return $this->db->query($sql);
	}
	
	function insert_jobwanted_hit($id){
		$this->load->database();
		$sql="INSERT INTO job_wanted_hits(job_wanted_id) VALUES(".$id.")";
		$this->db->query($sql);
	}
}
?>
